{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import requests\n",
    "import lxml.etree as le\n",
    "\n",
    "url = 'https://www.runoob.com/html/html-tutorial.html'\n",
    "# XPath语句\n",
    "x = '//a[@target=\"_top\"]'\n",
    "# 网站源码\n",
    "content = requests.get(url).content\n",
    "# 把HTML源码转成XML\n",
    "contentx = le.HTML(content)\n",
    "# 进行XPath数据提取\n",
    "rets = contentx.xpath(x)\n",
    "\n",
    "# 导出结果\n",
    "dic = {'label':[], 'link':[]}\n",
    "for ret in rets:\n",
    "    dic['label'].append(ret.text.strip())\n",
    "    dic['link'].append(\"www.runoob.com\" + ret.attrib.get('href'))\n",
    "df = pd.DataFrame(dic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>link</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HTML 教程</td>\n",
       "      <td>www.runoob.com/html/html-tutorial.html</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HTML 简介</td>\n",
       "      <td>www.runoob.com/html/html-intro.html</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HTML 编辑器</td>\n",
       "      <td>www.runoob.com/html/html-editors.html</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HTML 基础</td>\n",
       "      <td>www.runoob.com/html/html-basic.html</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HTML 元素</td>\n",
       "      <td>www.runoob.com/html/html-elements.html</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>HTML URL 编码</td>\n",
       "      <td>www.runoob.com/tags/html-urlencode.html</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>HTML 语言代码</td>\n",
       "      <td>www.runoob.com/tags/html-language-codes.html</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>HTTP 消息</td>\n",
       "      <td>www.runoob.com/tags/html-httpmessages.html</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>72</th>\n",
       "      <td>HTTP 方法</td>\n",
       "      <td>www.runoob.com/tags/html-httpmethods.html</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>键盘快捷键</td>\n",
       "      <td>www.runoob.com/tags/html-keyboardshortcuts.html</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>74 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          label                                             link\n",
       "0       HTML 教程           www.runoob.com/html/html-tutorial.html\n",
       "1       HTML 简介              www.runoob.com/html/html-intro.html\n",
       "2      HTML 编辑器            www.runoob.com/html/html-editors.html\n",
       "3       HTML 基础              www.runoob.com/html/html-basic.html\n",
       "4       HTML 元素           www.runoob.com/html/html-elements.html\n",
       "..          ...                                              ...\n",
       "69  HTML URL 编码          www.runoob.com/tags/html-urlencode.html\n",
       "70    HTML 语言代码     www.runoob.com/tags/html-language-codes.html\n",
       "71      HTTP 消息       www.runoob.com/tags/html-httpmessages.html\n",
       "72      HTTP 方法        www.runoob.com/tags/html-httpmethods.html\n",
       "73        键盘快捷键  www.runoob.com/tags/html-keyboardshortcuts.html\n",
       "\n",
       "[74 rows x 2 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel('小节名称与对应的链接.xls', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
